 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Entrance extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
	}
	
	//入口主页:如果登陆则跳出用户主页,否则则跳出登陆页面
	public function index()
	{
		if($this->session->userdata('login'))
			redirect('home');
		$a['login_key']=$this->user->get_key();	
		$this->load->view('entrance/login',$a);
	}
	
	//登陆接口
	public function login()
	{
		$rule=array(
				array(
					'field'=>'uname',
					'label'=>'uname',
					'rules'=>'trim|required|alpha_numeric',
					),
				array(
					'field'=>'pw',
					'label'=>'pw',
					'rules'=>'trim|required|alpha_numeric|xss_clean'
					),
			);
			
		$this->form_validation->set_rules($rule);
		
		if ($this->form_validation->run()==FALSE)
		{
			$this->index();
		}
		else
		{
			if($this->user->login($this->input->post('uname',TRUE),$this->input->post('pw',TRUE)))
			{
				$this->index();
			}
			else
			{ 
				$a['err_code']=$this->user->err_code;
				$a['err_msg']=$this->user->err_msg;
				$a['login_key']=$this->user->get_key();	
				$this->load->view('entrance/login',$a);

			}
		}
	}
	

	
	//注销登陆接口
	public function logout()
	{
		$this->user->logout();
		redirect(site_url('entrance'));
	}
	
	//注册页面
	public function register()
	{
		//rsa加密的mudulus
		
		$a['n']=$this->user->get_rsa_mudulus();
		$this->load->view('entrance/register',$a);
	}
		
	//注册接口
	public function regi()
	{
		$rule=array(
						array(
							'field'   => 'uname', 
							'label'   => 'UNAME', 
							'rules'   => 'trim|required|min_length[5]|max_length[12]'
						),
						array(
							'field'   => 'pword', 
							'label'   => 'PWORD', 
							//'rules'   => 'matches[pword_conf]'
						),  
						array(
							'field'   => 'email', 
							'label'   => 'EMAIL', 
							'rules'   => 'required|valid_email'
						),
						array(
							'field'   => 'cngname', 
							'label'   => 'CHINESE GIVEN NAME', 
							'rules'   => 'trim|required'
						),
						array(
							'field'   => 'cnfname', 
							'label'   => 'CHINESE FAMILY NAME', 
							'rules'   => 'trim|required'
						),
						array(
							'field'   => 'y', 
							'label'   => 'ENROLLED YEAR', 
							'rules'   => 'trim|required|numeric'
						)
		);
		$this->form_validation->set_rules($rule);

		//验证注册信息
		if ($this->form_validation->run() == FALSE)
		{
			$a['n']=$this->user->get_rsa_mudulus();
			$this->load->view('entrance/register',$a);
		}
		else
		{
			$pword = $this->input->post('pword');
			$uname = $this->input->post('uname');
			$email = $this->input->post('email');
			$y     = $this->input->post('y');
			$cnfn  = $this->input->post('cngname');
			$cnln  = $this->input->post('cnfname');
			
			$ck=$this->user->regi_chk($uname,$pword,$cnfn,$cnln,$y,$email);
			if (! $ck)
			{
				$a['err_code']=$this->user->get_err_code();
				$a['err_msg']=$this->user->get_err_msg();
				$a['n']=$this->user->get_rsa_mudulus();
				$this->load->view('entrance/register',$a);
			}
			else
			{
				$a['email_code']=$ck;
				$this->load->view('entrance/regi_sus',$a);
			}
		}
	}
	
	//邮箱验证接口
	public function email_veri()
	{
		if ($this->input->get('u'))
		{
			if ($this->user->regi_veri($this->input->get('u')))
			{
				//转跳成功页面
				$this->index();
			}
			else
			{
				$err['err_code']=$this->user->get_err_code();
				$err['err_msg']=$this->user->get_err_msg();
				$this->load->view('entrance/regi_fail',$err);
			}
		}
		else
		{
			$err['err_code']=403;
			$err['err_msg']="sorry I didn't get anything";
			$this->load->view('entrance/regi_fail',$err);
		}
	}
	
	//找回密码页面:
	public function receive()
	{
		$this->load->view('entrance/receive');
	}
	
	//找回密码提交处
	public function receivepw()
	{
		if ($this->input->post('uname') && $this->input->post('email'))
		{
			$url=$this->user->re_send($this->input->post('uname'),sha1($this->input->post('email')));
			if (! $url )
			{
				@$this->login_err();
			}
			else
			{
				$a['email_code']=$url;
				$this->load->view('entrance/regi_s',$a);
			}
		}
		else
		{
			$this->login_err('4134',"WHAT THE HELL THAT I DIDN'T RECIEVE ANYTHING!!!");
		}
	}
	
	public function email_receive()
	{
		if ($this->input->get('d') && $this->input->get('u') && $this->input->get('r'))
		{
			$ver=$this->user->re_verti($this->input->get('u'),$this->input->get('d'),$this->input->get('r'));
			if (! $ver)
			{
				@$this->re_err();
			}
			else
			{
				$this->load->view('entrance/login');
			}
		}
		else
		{
			$this->login_err('4134',"WHAT THE HELL THAT I DIDN'T RECIEVE ANYTHING!!!");
		}
	}
	
	public function pwreset()
	{
		if ($this->input->post('pword'))
		{
			$this->load->library('rsa');
			$this->rsa->load_publickey();
			$crypt=base64_encode($this->rsa->encrypt(sha1($this->input->post('pword'))));
			if ($this->user->re_setnp($crypt))
			{
			}
			else
			{
			}
		}
		else
		{
			$this->re_err('4134',"WHAT THE HELL THAT I DIDN'T RECIEVE ANYTHING!!!");
		}
	}
	
}
